﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TuongLai_Schools
{
    public partial class frmHocky : Form
    {
        public frmHocky()
        {
            InitializeComponent();
        }
        cls_DM_Hocky objHocky = new cls_DM_Hocky();
        private void frmHocky_Load(object sender, EventArgs e)
        {
            GetAll_Hocky();
        }
        private void GetAll_Hocky()
        {
            gridControl1.DataSource = objHocky.GetAll_Hocky();
        }
        private void Save_Hocky()
        {
            clsDM_Hocky objdmhk = new clsDM_Hocky();
            objdmhk.TenHocKy = gridView1.GetFocusedRowCellValue("TenHocKy").ToString();
            objdmhk.GhiChu = gridView1.GetFocusedRowCellValue("GhiChu").ToString();
            bool blnResult = false;
            if (ucDataButton1.DataMode == DataState.Insert)
            {
                objdmhk.HocKyID = 1;
                objdmhk.isChangeable = false;
                blnResult = objHocky.Insert(objdmhk);
            }
            else
            {
                objdmhk.HocKyID = (int)gridView1.GetFocusedRowCellValue("HocKyID");
                objdmhk.isChangeable = (bool)gridView1.GetFocusedRowCellValue("isChangeable");
                blnResult = objHocky.update(objdmhk);
            }

            if (blnResult)
            {
                clsCommon.SaveSuccessfully();
                ucDataButton1.DataMode = DataState.View;
                gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                gridView1.OptionsBehavior.Editable = false;
            }
            else
            {
                clsCommon.SaveNotSuccessfully();
                return;
            }
        }

        private void ucDataButton1_SaveHandler()
        {
            Save_Hocky();
        }

        private void ucDataButton1_DeleteHandler()
        {
            if (clsCommon.ConfirmDeletion(gridView1.GetFocusedRowCellValue("TenHocKy").ToString()) == DialogResult.No)
                return;
            objHocky.delete((int)gridView1.GetFocusedRowCellValue("HocKyID"));
            GetAll_Hocky();
        }

        private void ucDataButton1_InsertHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            gridView1.OptionsBehavior.Editable = true;
        }

        private void ucDataButton1_CancelHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gridView1.OptionsBehavior.Editable = false;
            GetAll_Hocky();
        }

        private void ucDataButton1_EditHandler()
        {
            gridView1.OptionsBehavior.Editable = true;
        }

    }
}
